from nodetypes.formats.rdf.Events import Document
from myelin.rdf.bindery.Model import OBJECT_TYPE_RESOURCE, OBJECT_TYPE_LITERAL
from myelin.rdf.Ns import RDFS_SEEALSO
from myelin.Ns import MYELIN, DC

from nodetypes.models.Document import Document as Model


class Document(Document):
    
    newDocumentModel = Model.Retrieve
    newDocumentDriver = staticmethod(Model.Initialize)
    
    def getStatements(self, context, consumer):
        eventData = context.event.data
        
        model = eventData.model
        params = eventData.params
        
        gas = self.getAgentStatements # fix me
        root = context.defaults.document_root
        
        locale = eventData.searchLocale or \
            context.preferences['locale']
        
        results = eventData.results
        if results:        
            href = context.urlFormatter.newUrlMapper(
                True,
                action = 'view'
            )
            F = context.repo.fetchResource
            newDocModel = self.newDocumentModel
            newDocDriver = self.newDocumentDriver
            uriScopeResolver = context.uriScopeResolver
        
            for r in eventData.results:
                
                n = F(r.path)
                d = n.getAbsolutePathAsUri()
                m = newDocModel(
                    newDocDriver(
                        n.getModel().driver,
                        uriScopeResolver = uriScopeResolver
                    ),
                    n.uri,
                    d,d
                )
                
                mrs = m.rdf_toStatements
                
                consumer(mrs('rdfType'))
                consumer(mrs('dcTitle'))
                consumer(mrs('dcSubject'))
                consumer(mrs('dcDescription'))
                consumer(mrs('dcLanguage'))
                consumer(mrs('dctermsCreated'))
                consumer(mrs('dctermsModified'))
                consumer(mrs('dcCreator'))
                consumer(mrs('mLast_Modifier'))
                
                for a in m.dcCreator:
                    gas(
                        context,
                        consumer,
                        a,
                        locale
                    )
                
                for a in m.mLast_Modifier:
                    gas(
                        context,
                        consumer,
                        a,
                        locale
                    )
                                   
                consumer((
                    (
                        m.rdf_value,
                        RDFS_SEEALSO,
                        href(m.rdf_domain),
                        '',
                        m.rdf_scope,
                        OBJECT_TYPE_RESOURCE,
                    ), (            
                        u'',
                        DC + u'source',
                        m.rdf_value,
                        None,
                        u'',
                        OBJECT_TYPE_RESOURCE
                    )
                ))
            
            header = eventData.resultsHeader
            consumer((
                (
                    u'',
                    MYELIN + u'range',
                    u'%d,%d' % (eventData.searchBaseIndex + 1,eventData.searchBaseIndex + header.size),
                    None,
                    u'',
                    OBJECT_TYPE_LITERAL
                ),(
                    u'',
                    MYELIN + u'total-results',
                    unicode(header.estimate),
                    None,
                    u'',
                    OBJECT_TYPE_LITERAL
                ),
            ))
                
            
        return consumer